package com.chinasoft04.mall.controller;

import com.chinasoft04.mall.service.AuthService;
import com.chinasoft04.mall.dto.AuthL;
import com.chinasoft04.mall.dto.AuthRoleDto;
import com.chinasoft04.mall.dto.AuthT;
import com.chinasoft04.mall.pojo.AuthMainTab;
import com.chinasoft04.mall.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/api/private/v1")
public class AuthController {

    //若有了token，则不需要在这里设置全局变量
    Integer mg_id = 500;

    @Autowired
    AuthService authService;

    @GetMapping("/menus")
    public Result getMenus(){
        String ps_ids = authService.getPsIdsByMgId(mg_id);
        List<AuthMainTab> mainTabByRid = authService.getMainTabByIds(ps_ids);
        return new Result().success(mainTabByRid, "获取菜单成功");
    }

    @GetMapping("/rights/{type}")
    public Result getAuth(@PathVariable String type){
        if(type.equals("list")){
            //解析token里的mg_id
            List<AuthL> authList = authService.getPermissionByList(mg_id);
            System.out.println(authList);
            if(authList!=null)return new Result().success(authList, "获取权限列表成功");
        }else if(type.equals("tree")){
            List<AuthT> authList = authService.getPermissionByTree();
            System.out.println(authList);
            if(authList!=null)return new Result().success(authList, "获取权限列表成功");
        }

        return new Result().failed("获取失败");
    }

    @GetMapping("/roles")
    public Result getAllRoles(){
        List<AuthRoleDto> allRoleWithChildren = authService.getAllRoleWithChildren();
        //对role进行权限更新，剔除多余的权限
        for (AuthRoleDto allRoleWithChild : allRoleWithChildren) {
            authService.checkAuthByRole(allRoleWithChild.getRoleId());
        }
        //重新获取新的roles
        allRoleWithChildren = authService.getAllRoleWithChildren();
        return new Result().success(allRoleWithChildren, "获取角色列表成功");
    }

    @PostMapping("/roles")
    public Result addRoles(@RequestBody AuthRoleDto role){
        boolean bl = authService.addRole(role);
        if(bl)return new Result().success(role, "创建成功", 201);
        return new Result().failed("创建角色失败");
    }

    @GetMapping("/roles/{id}")
    public Result getRoleById(@PathVariable Integer id){
        AuthRoleDto roleDto = authService.getRoleDtoByRoleIdWithoutChildren(id);
        if(roleDto==null)return new Result().failed("获取角色失败");
        return new Result().success(roleDto, "获取成功");
    }

    @PutMapping("/roles/{id}")
    public Result editRole(@RequestBody AuthRoleDto roleDto){
        System.out.println(roleDto);
        boolean bl = authService.updateRoleByRoleId(roleDto);
        if(bl)return new Result().success("修改成功");
        return new Result().failed("修改失败");
    }

    @DeleteMapping("/roles/{id}")
    public Result deleteRoleById(@PathVariable Integer id){
        boolean bl = authService.deleteRoleByRoleId(id);
        if(bl)return new Result().success("删除成功");
        return new Result().failed("删除失败");
    }

    @PostMapping("/roles/{roleId}/rights")
    public Result replaceAuthByRidsAndRoleId(@PathVariable Integer roleId, @RequestBody Map<String, String> rids){
//        System.out.println(roleId);
//        System.out.println(rids);
//        System.out.println(rids.get("rids"));
//        System.out.println(rids.get("rids").getClass().getTypeName());
        boolean bl = authService.replaceAuthByRidsAndRoleId(roleId, rids.get("rids"));
        if(bl)return new Result().success("授权替换成功");
        return new Result().failed("授权替换失败");
    }

    @DeleteMapping("/roles/{roleId}/rights/{rightId}")
    public Result deleteAAuthByRoleIdAndRightId(@PathVariable Integer roleId, @PathVariable Integer rightId){
        System.out.println(roleId);
        System.out.println(rightId);
        boolean bl = authService.deleteAAuthByRoleIdAndRightId(roleId, rightId);
        if(bl)return new Result().success("删除指定权限成功");
        return new Result().failed("删除指定权限失败。");
    }
}
